﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; // FNFException
using System.Runtime.InteropServices;

namespace LANmine
{
    public partial class MainForm : Form
    {
        /** Enable Windows 7-themed elements */
        [DllImport("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
        private static extern int SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);

        /** Create Class Variables for every screen */
        Form frmHome;
        Form frmCatering;
        Form frmCompos;
        Form frmPickups;
        Form frmSeats;
        Form frmProfile;
        Form frmCheckin;
        Form frmAdmin;
        Form frmAbout;
        Form frmProperties;

        /** Holds the user profile */
        private User lmUser;

        /** Language object */
        private Language lmLang;

        /**
         * MainForm::Constructor
         * Saves the user object, safely instantiates the language handler and spawns all subforms.
         * !Application will exit if the language file is not found!
         */
        public MainForm(User lmUser)
        {
            /** Save the user object */
            this.lmUser = lmUser;
            
            /**
             * Create an instance of the Language handler, but make sure the corresponding .resx file exists.
             * Application will force close if this is not the case.
             */
            try
            {
                /** Spawn the language handler with the appropriate language */
                lmLang = new Language(lmUser.Language);
            }
            catch (FileNotFoundException)
            {
                /** Show a detailed dialog box */
                MessageBox.Show("A required language resource file was not found.\nPlease contact support, LANmine will now exit.",
                    "Resource file not found!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);

                /** Exit the application, cannot continue */
                this.Close();
                Environment.Exit(0);
            }

            /**
             * Interface Initialization
             */
            InitializeComponent();
            SpawnForms();
            ApplyLanguage();
        }

        /**
         * MainForm::SpawnForms()
         * Initialize all forms needed by the current user
         */
        private void SpawnForms()
        {
            /**
             * MainForm
             */

            /** Start the clock */
            this.tmrClock.Start();
            UpdateClock();

            /** Set the username in the header */
            this.lblUserName.Text = lmUser.UserName;

            /**
             * Default Forms
             */

            /** Home */
            this.frmHome = new HomeForm(this.lmLang);
            this.frmHome.MdiParent = this;
            this.frmHome.Text = "Home";
            this.frmHome.Show();

            /** Catering */
            this.frmCatering = new CateringForm(this.lmLang, this.lmUser);
            this.frmCatering.MdiParent = this;
            this.frmCatering.Text = "Catering";
            this.frmCatering.Show();

            /** Compos */
            this.frmCompos = new ComposForm(this.lmLang, this.lmUser);
            this.frmCompos.MdiParent = this;
            this.frmCompos.Text = "Competitions";
            this.frmCompos.Show();

            /** Pickups */
            this.frmPickups = new PickupsForm(this.lmLang);
            this.frmPickups.MdiParent = this;
            this.frmPickups.Text = "Pickups";
            this.frmPickups.Show();

            /** Seats */
            this.frmSeats = new SeatsForm(this.lmLang);
            this.frmSeats.MdiParent = this;
            this.frmSeats.Text = "Seats";
            this.frmSeats.Show();

            /** Profile */
            this.frmProfile = new ProfileForm(this.lmLang, this.lmUser);
            this.frmProfile.MdiParent = this;
            this.frmProfile.Text = "Edit Profile";
            this.frmPickups.Show();

            /**
             * Admin-Only Forms
             * Use an abstract user class to determine whether the user has the right to open these forms or not.
             * If the user is an admin, initialize the forms and enable the buttons.
             */
            if (this.lmUser.IsStaff == true)
            {
                /** Enable the buttons */
                this.btnAdmin.Enabled = true;
                this.btnCheckin.Enabled = true;

                /** Check-in booth */
                this.frmCheckin = new CheckinForm(this.lmLang);
                this.frmCheckin.MdiParent = this;
                this.frmCheckin.Text = "LANparty Check-in";
                this.frmCheckin.Show();

                /** Application administration */
                this.frmAdmin = new AdminForm(this.lmLang);
                this.frmAdmin.MdiParent = this;
                this.frmAdmin.Text = "LANparty Administration";
                this.frmAdmin.Show();
            }
        }

        #region Language Specifications
        /**
         * MainForm::ApplyLanguage()
         * ---------------------------
         * Overwrite the text property of all elements in this form with the right ones.
         */
        private void ApplyLanguage()
        {
            /**
             * MainForm - Navigation
             */
            this.btnCatering.Text = this.lmLang.GetString("menu.btnCatering");
            this.btnCompos.Text = this.lmLang.GetString("menu.btnCompos");
            this.btnPickups.Text = this.lmLang.GetString("menu.btnPickups");
            this.btnSeats.Text = this.lmLang.GetString("menu.btnSeats");
            this.btnProfile.Text = this.lmLang.GetString("menu.btnProfile");
            this.btnCheckin.Text = this.lmLang.GetString("menu.btnCheckin");
            this.btnAdmin.Text = this.lmLang.GetString("menu.btnAdmin");
            this.lblCredits.Text = this.lmLang.GetString("menu.lblCredits");
        }
        #endregion

        /**
         * MainForm::SwitchWindow()
         * Display the given Form object in the MDI space
         */
        private void SwitchWindow(Form TargetForm)
        {
            if(this.ActiveMdiChild != null)
                this.ActiveMdiChild.SendToBack();

            /** Display the given MDI child */
            TargetForm.Show();
            TargetForm.BringToFront();
            TargetForm.MdiParent = this;
            TargetForm.Activate(); // Using activate, not show!
            TargetForm.Refresh();
        }

        /**
         * MainForm::UpdateClock()
         * Update the clock at the top of the form
         */
        private void UpdateClock()
        {
            this.lblTime.Text = DateTime.Now.ToLongTimeString();
        }

        /**
         * LANmine::MainForm
         * Event Triggers
         */
        #region Event Triggers
        /** Make time progress :) */
        private void tmrClock_Tick(object sender, EventArgs e)
        {
            UpdateClock();
        }

        /** Switch to Home */
        private void btnHome_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmHome);
        }

        /** Switch to Catering */
        private void btnCatering_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmCatering);
        }

        /** Switch to Compos */
        private void btnCompos_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmCompos);
        }

        /** Switch to Pickups */
        private void btnPickups_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmPickups);
        }

        /** Switch to Seats */
        private void btnSeats_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmSeats);
        }

        /** Switch to Profile */
        private void btnProfile_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmProfile);
        }

        /** Switch to Check-in */
        private void btnCheckin_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmCheckin);
        }

        /** Switch to Administration */
        private void btnAdmin_Click(object sender, EventArgs e)
        {
            SwitchWindow(this.frmAdmin);
        }

        /** Terminate the application */
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            /** Exit the application */
            Environment.Exit(0);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            /** Display the home form */
            SwitchWindow(this.frmHome);
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            //spawn AboutForm as Dialog

            this.frmAbout = new AboutForm(lmLang);
            this.frmAbout.Text = "About LANmine";
            this.frmAbout.ShowDialog();
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            //spawn PropertiesForm as Dialog

            this.frmProperties = new PropertiesForm(lmLang);
            this.frmProperties.Text = "Properties";
            this.frmProperties.ShowDialog();
        }
    }

        #endregion

        
}